/* $RCSFile: ModuleDefsAll.ascx.cs $
 * Apache license v2 2011 by Christian Surieux
 * $Source: ?/AddonNice/PageModules/ModuleDefsAll/ModuleDefsAll.ascx.cs $
 * $log$
 * Revision 20 2010/08/09 10:51:21  christian.surieux
 *   Added Log comment to all .aspx .ascx .master files
 * Revision 19 2010/08/09 09:43:56  christian.surieux
 *   Added Log to cs sources files
 * Revision 6 2010/08/06 19:56:26  christian.surieux
 *   Added Log to All cs files
 */

using System;
using System.Collections;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Globalization;

using AddonNice.Core;
using AddonNice.Diagnostics;
using AddonNice.Settings;
using AddonNice.UI;
using AddonNice.UI.DataTypes;
using AddonNice.UI.WebControls;
using AddonNice.Security;
using AddonNice.Configuration;
using AddonNice.Configuration.Settings;

using Telerik.Web.UI;

using Trace2    =   System.Diagnostics.Trace;

namespace AddonNice.PageModules.ModuleDefsAll
{
    public class MDA
    {
        public const string ShowUsage  =   "MODULEDEFSALL_SHOWUSAGE";
        public const string PageSize   =   "MODULEDEFSALL_PAGE_SIZE";
    }

    /// <summary>
    /// Initializer class for ModuleDefsAll 
    /// </summary>
    public class ModuleDefsAllSI : PortalModuleControlSI
    {
        public ModuleDefsAllSI(ModuleSettingsDictionary bSettings,ISettingsOwner pm) : base()
        {
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]ModuleDefsAllSI Ctor BEG.",ModuleTraceSwitch.Sw.Info);
            InitSettings(bSettings,pm);
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]ModuleDefsAllSI Ctor END.",ModuleTraceSwitch.Sw.Info);
        }
 
        public override void InitSettings(ModuleSettingsDictionary bSettings,ISettingsOwner pm)
        {             
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]ModuleDefsAllSI InitSettings BEG.",ModuleTraceSwitch.Sw.Info);
            
            base.InitSettings(bSettings,pm);

            string extgrp                   =   pm.ExtendedResourcesGroup;

			SettingItemSpecial ShowUsage	=	new SettingItemSpecial(MDA.ShowUsage,new BooleanDataType());
			ShowUsage.Order			        =	10;
            ShowUsage.Value			        =	"True";
			ShowUsage.ExtendedGroupName     =   extgrp;
			ShowUsage.Description		    =	"Check to display Module usage";
            ShowUsage.EnglishName		    =	"Show Module usage";
			bSettings.AddSetting(ShowUsage);


            SettingItemSpecial PageSz	    =	new SettingItemSpecial(MDA.PageSize,new IntegerDataType());
			PageSz.Order			        =	20;
            PageSz.Value			        =	"20";
			PageSz.ExtendedGroupName        =   extgrp;
			PageSz.Description		        =	"Enter the grid page Size";
            PageSz.EnglishName		        =	"Page Size";
			bSettings.AddSetting(PageSz);

            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( "[V]ModuleDefsAllSI InitSettings END.",ModuleTraceSwitch.Sw.Info);
        }
    }

    /// <summary>
    /// Control to show/edit portals modules (AdminAll)
    /// </summary>
    public partial class ModuleDefsAll : PortalModuleControl 
    {
        public override Guid GuidID 
		{
			get
			{
				return new Guid("{D04BB5EA-A792-4E87-BFC7-7D0ED3ADD582}");
			}
		}

        /// <summary>
		/// Admin Module
		/// </summary>
		public override bool AdminModule
		{
			get
			{
				return true;
			}
		}

        public bool ShowUsage
        {
            get
            {
                return Settings[MDA.ShowUsage].ToBool();
            }
        }

        public int GridPageSize
        {
            get
            {
                return Settings[MDA.PageSize].ToInt();
            }
        }

        /// <summary>
        /// CTor
        /// </summary>
        public ModuleDefsAll()
        {
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine("[V]ModuleDefsAll Ctor BEG",ModuleTraceSwitch.Sw.Info);
            
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine("[V]ModuleDefsAll Ctor END.",ModuleTraceSwitch.Sw.Info);
        }
        
        public override string ExtendedResourcesGroup 
		{
            get
            {
       			const string extgrp                 =   "MODULEDEFSALL";
                return extgrp;
            }
		}

        public override void InitPortalModuleControl(ModuleSettingsDictionary bSettings)
        {
            PortalModuleInitializer =   new ModuleDefsAllSI(bSettings,this);
        }

        /// <summary>
		/// Populate the current defs settings from the configuration system
		/// </summary>
        private void Page_Load(object sender, System.EventArgs e) 
        {
            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ModuleDefsAll Page_Load BEG IsPosBack: {0}, EnableViewState: {1}", IsPostBack,EnableViewState),ModuleTraceSwitch.Sw.Info);
            if (!IsPostBack)
            {
                RadGrid1.PageSize =    GridPageSize;
            }

            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine("[V]ModuleDefsAll Page_Load END.",ModuleTraceSwitch.Sw.Info);
        }
    
		/// <summary>
		/// Read Definitions in DB
		/// </summary>
        void RadGrid1_NeedDataSource(object source, Telerik.Web.UI.GridNeedDataSourceEventArgs e)
        {
            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ModuleDefsAll RadGrid1_NeedDataSource RebindReason: {0}", e.RebindReason),ModuleTraceSwitch.Sw.Info);
            // Get the portal's defs from the database
            try
            {
                RadGrid1.DataSource         =   new ModulesDB().GetModuleDefinitions();
            }
            catch (Exception ex)
            {
                if (ModuleTraceSwitch.Sw.TraceError)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ModuleDefsAll RadGrid1_NeedDataSource ex: {0}", ex),ModuleTraceSwitch.Sw.Info);
                throw;
            }
        }

        void RadGrid1_UpdateCommand(object source,GridCommandEventArgs e)
        {
            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ModuleDefsAll RadGrid1_EditCommand CommandName: {0}", e.CommandName),ModuleTraceSwitch.Sw.Info);
            Guid GeneralModDefID    =   (Guid)e.Item.OwnerTableView.DataKeyValues[e.Item.ItemIndex]["GeneralModDefID"];
            // Go to edit page
            string strUrl           =   HttpUrlBuilder.BuildUrlWithCustomAttributes(Config.ModulesPath + "/ModuleDefsAll/ModuleDefinitionsEdit.aspx",PageID,"DefID=" + GeneralModDefID + "&Mid=" + ModuleID);
            if ( ModuleTraceSwitch.Sw.TraceVerbose )
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ModuleDefsAll RadGrid1_EditCommand strUrl: {0}",strUrl),ModuleTraceSwitch.Sw.Info);
            ADNPage.Redirect(strUrl,true);
        }

        void RadGrid1_DeleteCommand(object source,GridCommandEventArgs e)
        {
		    if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ModuleDefsAll RadGrid1_DeleteCommand BEG CommandName: {0}",e.CommandName),ModuleTraceSwitch.Sw.Info);
		    try
		    {
                GridDataItem item   =   (GridDataItem)e.Item;   
                string DesktopSrc   =   item.OwnerTableView.DataKeyValues[item.ItemIndex]["DesktopSrc"].ToString();
		        if (ModuleTraceSwitch.Sw.TraceVerbose)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ModuleDefsAll RadGrid1_DeleteCommand DesktopSrc: {0}",DesktopSrc),ModuleTraceSwitch.Sw.Info);
                Hashtable instalTb  =   new Hashtable();
		        AddonNice.Helpers.ModuleInstall.Uninstall(instalTb,DesktopSrc,true,true);
		    }
		    catch(Exception ex)
		    {
			    if (ModuleTraceSwitch.Sw.TraceError)
                    Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[E]ModuleDefsAll RadGrid1_DeleteCommand ex : {0}",ex),ModuleTraceSwitch.Sw.Info);
		    }
		}

        const string BtnUpdateName          =   "UpdateButton";
        protected void RadGrid1_ItemCreated(object sender, Telerik.Web.UI.GridItemEventArgs e)
        {
            if (e.Item is GridDataItem)  
            {
                GridDataItem gItem          =   e.Item as GridDataItem;
                ImageButton originalButton  =   gItem[BtnUpdateName].Controls[0] as ImageButton;  
                ImageButton newButton       =   new ImageButton();  
                newButton.SkinID            =   "EditBtn"; // StdEdit Btn 
                newButton.CommandArgument   =   originalButton.CommandArgument;  
                newButton.CommandName       =   originalButton.CommandName;  
                newButton.AlternateText     =   originalButton.AlternateText;  
                newButton.ID                =   originalButton.ID;
                gItem[BtnUpdateName].Controls.RemoveAt(0);  
                gItem[BtnUpdateName].Controls.Add(newButton);  
                }
        }		
        #region Std overrides

        public override void Page_PreInit(object sender, EventArgs e)
        {
            // Add a link for the edit page
            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ModuleDefsAll Page_PreInit BEG IsPosBack: {0}", IsPostBack),ModuleTraceSwitch.Sw.Info);
            AddUrl                  =   Config.ModulesPath + "/ModuleDefsAll/ModuleDefinitionsEdit.aspx";
            base.Page_PreInit(sender, e);
            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine("[V]ModuleDefsAll Page_PreInit END.",ModuleTraceSwitch.Sw.Info);
        }


        override protected void OnInit(EventArgs e)
        {
            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine( string.Format(CultureInfo.InvariantCulture,"[V]ModuleDefsAll OnInit BEG IsPosBack: {0}", IsPostBack),ModuleTraceSwitch.Sw.Info);
			InitializeComponent();
            base.OnInit(e);
            if (ModuleTraceSwitch.Sw.TraceVerbose)
                Trace2.WriteLine("[V]ModuleDefsAll OnInit END.",ModuleTraceSwitch.Sw.Info);
		}

        private void InitializeComponent() 
        {
            RadGrid1.NeedDataSource +=  new GridNeedDataSourceEventHandler(RadGrid1_NeedDataSource);
            RadGrid1.UpdateCommand  +=  new GridCommandEventHandler(RadGrid1_UpdateCommand);
            RadGrid1.DeleteCommand  +=  new GridCommandEventHandler(RadGrid1_DeleteCommand);
            RadGrid1.ItemCreated    +=  new GridItemEventHandler(RadGrid1_ItemCreated);
			Load                    +=  new EventHandler(Page_Load);
		}

        #endregion

    }
}